const _ = require("lodash");

const acQueue = require(__base + '/lib/account-creation-queue');

const models = require(__base + '/models');
const errcode = require(__util + "/errcode");
const ct = require(__util + "/catch"); // 路由错误捕获(不使用则不会捕获异常错误)

module.exports = ct(async (req, res) => {
  const { billNoList } = req.body

  const accountList = await fetchAccountList(billNoList)

  for (const acReq of accountList) {
    acReq.status = 'paid-recreate'
    await acReq.save();
    acQueue.add(acReq);
  }

  return res.json(
    errcode.SUCCESS()
  );
});

// 更新账号状态 (修改为重新创建)
const fetchAccountList = async (billNoList) => {
  return await models.AccountCreationRequest.findAll({
    where: {
      billNo: billNoList
    }
  })
}